<?php
namespace Home\Logic;
use Think\Model\RelationModel;

/**
 * 分类逻辑定义
 * Class CatsLogic
 * @package Home\Logic
 */

class DistributLogic extends RelationModel
{

    /**
     * 分销记录逻辑
     */
    public function rebate_log($order)
    {
    
        $order_id = $order['order_id'];
        if(!$order_id){
            return "order_id为空";
            exit();
        }

        //找出购买者的 first_leader，second_leader，third_leader
        $user_id = $order['user_id'];
        $data = M('users')->where(array('user_id'=>$user_id))->field('first_leader,second_leader,third_leader')->find();

        M('order')->where(array('order_id'=>$order_id))->save($data);

        $ex =  M('distribut')->where(array('order_id'=>$order_id))->find();
       
        if(!$ex){
            $data['order_id'] = $order_id;
            M('distribut')->add($data);
        }
       
        //写入rebate_log
        if((int)$data['first_leader'] > 0){
            $res['first_leader'] = $this->save_rebate_log($order_id,$data['first_leader'],1);
        }else{
            $res['first_leader'] = "无first_leader";
        }

        if((int)$data['second_leader'] > 0){
            $res['second_leader'] = $this->save_rebate_log($order_id,$data['second_leader'],2);
        }else{
            $res['second_leader'] = "无second_leader";
        }

        if((int)$data['third_leader'] > 0){
            $res['third_leader'] = $this->save_rebate_log($order_id,$data['third_leader'],3);
        }else{
            $res['third_leader'] = "无third_leader";
        }

    
        return json_encode($res);
    }

  
    /**
     * 写入
     */
     public function save_rebate_log($order_id,$user_id,$grade){
        header("Content-type:text/html;charset=utf-8");
       
        // user_id  为  获佣金 的ID
        //判断是否存在
        $cz = M('rebate_log')->where(array('user_id'=>$user_id,'order_id'=>$order_id))->find();
        if(!$cz){
            //不存在
            $order = M('order')->where(array('order_id'=>$order_id))->find();
            $data['buy_user_id'] = $order['user_id'];
            $data['user_id'] = $user_id;
            $data['nickname'] = $order['consignee'];
            $data['order_sn'] = $order['order_sn'];
            $data['order_id'] = $order_id;
            $data['goods_price'] = $order['goods_price'];

            //$grade   1   2    3
            $store_id = $order['store_id'];

            $store_disttribut = M('store_distribut')->where(array('store_id'=>$store_id))->find();
          

            if(!$store_disttribut ){
                return "该店无分销";
            }
            //该店没分销

            //该店关闭分销
            if($store_disttribut['switch'] == 0 ){
                return "该店关闭分销功能";
            }

            $data['money'] =  $this->get_money_by_order_id($order_id,$grade,$store_disttribut);
          
            //获佣金额为空，就不要插入了
            if((float)$data['money'] == 0){
                return "获佣金额为空";
            }

            //获佣用户级别
            $data['level'] = M('users')->where(array('user_id'=>$data['user_id']))->getField('level');
            $data['create_time'] = time();
            $data['status'] = 0;
            $data['store_id'] = $store_id;

            M('rebate_log')->add($data);
            return "写入成功";
            
        }
     }


     /**
      * 通过 order_id +  grade  获取 分佣金额
      */
      private function get_money_by_order_id($order_id,$grade, $store_disttribut){
        $order_ids = M('order_goods')->where(array('order_id'=>$order_id))->select();
        foreach($order_ids as $v){
            $goods_id = $v['goods_id'];
            $distribut = M('goods')->where(array('goods_id'=>$goods_id))->getField('distribut');
                switch ($grade)
                {
                    case 1:
                        $data['money'] = $data['money'] + ( $store_disttribut['first_rate'] / 100 ) * $distribut;
                        break;
                    case 2:
                        $data['money'] =  $data['money'] + ( $store_disttribut['second_rate'] / 100 ) * $distribut;
                        break;
                    case 3:
                        $data['money'] =  $data['money'] + ( $store_disttribut['third_rate'] / 100 ) * $distribut;
                        break;
                
                    default:
                        $data['money'] =  0;
                }
        }
        return $data['money'];
        
    }

    /**
      * 自动分成 符合条件的 分成记录
      */
      function auto_confirm(){
         
        $switch = tpCache('distribut.switch');
       
        if($switch == 0)
            return false;
        
        $today_time = time();
       
        $distribut_date = tpCache('distribut.date');
      
        $distribut_time = $distribut_date * (60 * 60 * 24); // 计算天数 时间戳
        $rebate_log_arr = M('rebate_log')->where("status = 2 and ($today_time - confirm) >  $distribut_time")->select();
       
        foreach ($rebate_log_arr as $key => $val)
        {
            accountLog($val['user_id'], $val['money'], 0,"订单:{$val['order_sn']}分佣",$val['money']);             
            $val['status'] = 3;
            $val['confirm_time'] = $today_time;
            $val['remark'] = $val['remark']."满{$distribut_date}天,程序自动分成.";
            M("rebate_log")->where("id", $val['id'])->save($val);
        }
    }


}